package com.cloudwise.filter;

import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/9/17 11:35
 * @description：删除操作过滤器
 * @modified By：
 * @version: 1.0
 */
//@WebFilter(urlPatterns = "/*")
public class DeleteFilter  implements Filter {
    Logger logger = Logger.getLogger(DeleteFilter.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String method = servletRequest.getParameter("method");
        String id = servletRequest.getParameter("id");
        System.out.println("操作方法："+method);
        if("delete".equals(method)){
            //处理删除操作，将删除操作的用户和事件以及删除的id记录在log日志中
                HttpServletRequest httpServletRequest= (HttpServletRequest) servletRequest;
                Object sessionUseranme = httpServletRequest.getSession().getAttribute("sessionUseranme");
                logger.info("用户"+sessionUseranme+getTime()+"删除了id为"+id+"的数据");
        }
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {

    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description:获取当前的字符串时间  yyyy-MM-dd HH:mm:SS
     * @create time: 2021/9/17 11:44
     * @param
     * @return java.lang.String
     */
    public String getTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        String format = sdf.format(new Date());
        return format;
    }
}
